import Router from 'koa-router'
import { LowCode } from '../../../../common/typings/lowCode'
import httpErrors from '../../../../core/HttpException'
import Auth from '../../../../middlewares/Auth'
import validator from '../../../../middlewares/validator'
import Mysql from '../../../../core/Mysql'
import getListByParentId from '../../../../common/apiValidate/lowCode/componentCategoryManager/list'
import { lineToHumpObject } from '../../../../common/utils/utils'
import Result from '../../../../common/utils/Result'
import Config from '../../../../config/Config'

const router = new Router({
  prefix: `${Config.api_prefix}lowCode/componentCategoryManager`,
})

/*
 * 查看当前路径下组件和分类
 * @return
 */
router.post('/list', Auth.verifyTokenPermission, validator(getListByParentId, 'body'), async (ctx) => {
  const { params, pageNum, pageSize } = ctx.request.body
  const { name = '', parentId = '' } = params
  const { list: componentList, total: componentTotal } = await getList('component', pageNum, pageSize, parentId, name)
  const { list: categoryList, total: categoryTotal } = await getList(
    'component_category',
    pageNum,
    pageSize,
    parentId,
    name
  )

  const records = [
    ...categoryList.map((item) => {
      return { ...item, type: 2 }
    }),
    ...componentList.map((item) => {
      return { ...item, type: 1 }
    }),
  ]

  const data = Result.getPagination(records, componentTotal + categoryTotal, pageSize, pageNum)
  throw new httpErrors.Success(data)
})

export default router

async function getList(tableName: string, pageNum: number, pageSize: number, parentId?: number, name = '') {
  const parentIdStr = `parent_id = ${parentId}`
  const nameStr = name ? `And name LIKE '%${name}%'` : ''
  const res = (
    await Mysql.command(`
    (
      SELECT
        *
      FROM
        ${tableName}
      WHERE
        ${parentIdStr}
        ${nameStr}
        LIMIT ${pageNum - 1}, ${pageSize}
    )
      ORDER BY
        updated_at DESC;
      SELECT
        count(*) FROM ${tableName};
  `)
  ).results
  const list: LowCode.Component[] = []
  for (const key in res[0]) {
    list.push(res[0][key])
  }
  const total: number = res[1][0]['count(*)']
  return {
    list: list.map(lineToHumpObject),
    total,
  }
}
